package com.example.springboot_test.easyexcel.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.example.springboot_test.common.poi.util.POIExcelUtil;
import com.example.springboot_test.easyexcel.model.PictureModel;
import com.example.springboot_test.easyexcel.model.common.SheetModel;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义图片处理器
 *
 * @author xudongmaster
 */
public class CustomPictureHandler implements SheetWriteHandler {

    /**
     * sheet页名称列表
     */
    private List<String> sheetNameList;
    /**
     * 图片信息
     */
    private List<PictureModel> pictureList = new ArrayList<>();


    public CustomPictureHandler(List<PictureModel> pictureList) {
        if (CollUtil.isEmpty(pictureList)) {
            return;
        }
        this.pictureList = pictureList.stream().filter(x ->
                StrUtil.isNotBlank(x.getSheetName()) && x.getPictureBytes() != null && x.getPictureBytes().length > 0)
                .collect(Collectors.toList());
        sheetNameList = this.pictureList.stream().map(SheetModel::getSheetName).distinct().collect(Collectors.toList());
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    /**
     * sheet页创建之后调用
     *
     * @param writeWorkbookHolder 文档对象
     * @param writeSheetHolder sheet页对象
     */
    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        //不需要添加图片，或者当前sheet页不需要添加图片
        if (CollUtil.isEmpty(pictureList) || !sheetNameList.contains(sheet.getSheetName())) {
            return;
        }
        //获取当前sheet的图片
        List<PictureModel> sheetPictureList = pictureList.stream().filter(x ->
                StrUtil.equals(x.getSheetName(), sheet.getSheetName())
        ).collect(Collectors.toList());
        //当前sheet页不需要图片
        if (CollUtil.isEmpty(sheetPictureList)) {
            return;
        }
        for (PictureModel pictureModel : sheetPictureList) {
            //图片数据
            byte[] pictureBytes = pictureModel.getPictureBytes();
            //插入图片
            POIExcelUtil.insertImg(writeWorkbookHolder.getWorkbook(), sheet, pictureBytes, pictureModel.getStartRowIndex()
                    , pictureModel.getEndRowIndex(), pictureModel.getStartColumnIndex(), pictureModel.getEndColumnIndex()
                    , (pictureModel.getPictureType() == null ? Workbook.PICTURE_TYPE_JPEG : pictureModel.getPictureType()));
        }
        //删除图片信息
        pictureList.removeAll(sheetPictureList);
        sheetNameList = pictureList.stream().map(SheetModel::getSheetName).distinct().collect(Collectors.toList());
    }
}